package libs

import (
	"bytes"
	"encoding/base64"
	"io"
	"os"
	"path"
	"strings"
)

func SaveBase64ImageToFile(image string, folder string) (string, error) {
	CreateDirectoryIfNotExist(folder)
	image = strings.Split(image, ",")[1]
	decodedBytes, err := base64.StdEncoding.DecodeString(image)
	if err != nil {
		return "", err
	}
	filePath := path.Join(folder, GenerateSessionID()+".png")
	fw, err := os.Create(filePath)
	if err != nil {
		return "", err
	}
	defer fw.Close()

	_, err = io.Copy(fw, bytes.NewReader(decodedBytes))
	return filePath, err
}
